using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using System.Runtime.Serialization;
namespace Rogue
{
    [Serializable]
    public class Human : Entity
    {
        // The hero right now is a generic human, but could be any other type of entity.

        public Human(string a_sName)
            : base(a_sName)
        {
            AddHumanBody();
        }

        public Human()
            : base()
        {
            AddHumanBody();
        }

        // we don't give the kick attack to the Leg class, because only some legged creatures should kick
        static Attack s_Kick = new Attack("kick", "kicks", "legs", 5, DamageType.Bash, Attack.Priority.Low);
        static Attack s_Punch = new Attack("punch", "punches", "arms", 10, DamageType.Bash, Attack.Priority.Normal);

        private void AddHumanBody()
        {
            m_BodyRoot = new Torso(this);
            Head head = new Head(m_BodyRoot);
            Arm leftArm = new Arm(m_BodyRoot, "left");
            Arm rightArm = new Arm(m_BodyRoot, "right");
            leftArm.GrantedAttack = s_Punch;
            rightArm.GrantedAttack = s_Punch;

            Leg leftLeg = new Leg(m_BodyRoot, "left");
            Leg rightLeg = new Leg(m_BodyRoot, "right");
            leftLeg.GrantedAttack = s_Kick;
            rightLeg.GrantedAttack = s_Kick;

            // callbacks for losing body parts
            m_BodyRoot.OnSubPartLoss += SubPartLossHandler;
            m_BodyRoot.OnSubPartHealthChange += BodyHealthChange;

            m_BodyRoot.InitAttackIndices();

            m_BodyRoot.MultMaxHealth(4.0f);     // for the hero only, really...
        }

        // this is what can kill us.
        private void SubPartLossHandler(BodyPart a_Part)
        {
            if (a_Part.GetType() == typeof(Head) || a_Part.GetType() == typeof(Torso))
            {
                m_IsAlive = false;
            }

            a_Part.MakeSeveredPart(this);
        }

        void BodyHealthChange(BodyPart a_Part, BodyPart.HealthChangeType a_Type, int a_DeltaHealth)
        {
            if (a_Type == BodyPart.HealthChangeType.Bleed)
            {
                RogueGame.Instance.Map.BloodyTerrain(X, Y, true);
            }
        }

        // images for the paperdoll
        static DetailWindowImages s_HumanDetailWindowImages = new DetailWindowImages("human");
        public override DetailWindowImages GetDetailWindowImages()
        {
            return s_HumanDetailWindowImages;
        }

        // how fast a person moves with different numbers of missing legs
        public override int GetMoveDelay()
        {
            int numLegs = m_BodyRoot.GetNumParts<Leg>();
            switch (numLegs)
            {
                case 0: return m_nDelay * 10;
                case 1: return m_nDelay * 2;
                case 2: return m_nDelay;
            }

            return 1 + (m_nDelay - 1) / numLegs;
        }

        #region Serialization
        public Human(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            InputManager.Instance.OnKeyPressed += new InputManager.OnKeyPressedHandler(OnKeyPressed);
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
        #endregion
    }
}


